﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOTemplatedGenerator.Models;

namespace DTOTemplatedGenerator.ViewModels
{
    public class PropertyModel: BaseVM
    {
        private PropTemplateModel _template;
        private string _transType;
        public bool IsSelected { get; set; }

        public const string NName = "Name";
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name == value)
                    return;
                _Name = value;
                OnPropertyChanged(NName);
            }
        }
        public const string NType = "Type";
        private string _Type;
        public string Type
        {
            get { return _Type; }
            set
            {
                if (_Type == value)
                    return;
                _Type = value;
                OnPropertyChanged(NType);
            }
        }

        //Aux info
        public const string NAttr = "Attr";
        private string _Attr;               
        public string Attr
        {
            get { return _Attr; }
            set
            {
                if (_Attr == value)
                    return;
                _Attr = value;
                OnPropertyChanged(NAttr);
            }
        }        
                
        public bool IsField { get; set; }

        public PropTemplateModel Template
        {
            get { return _template; }
            set
            {
                _template = value;
                OnPropertyChanged("Template");
                if (Type != null && _template != null)
                {
                    if (_template.TypesMatch != null)
                    {
                        var transType = ViewModel.Instance.TypesTrans.FirstOrDefault(tt => tt.Name == _template.TypesMatch);
                        if (transType != null)
                        {
                            var match = transType.Matchs.FirstOrDefault(m => {
                                if (m.Source.Contains('*'))
                                    return Type.IndexOf(m.Source.Replace("*", ""), StringComparison.InvariantCultureIgnoreCase) >= 0;
                                return m.Source.Equals(Type, StringComparison.InvariantCultureIgnoreCase); 
                            });
                            if (match != null)
                                TransType = match.Match;
                            else
                                TransType = Type;
                        }
                    }
                    else
                        TransType = Type;
                }
            }
        }

        public string TransType 
        {
            get { return _transType; }
            set 
            {
                _transType = value;
                OnPropertyChanged("TransType");
            }
        }
    }
}
